package com.knowledgepoint.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.knowledgepoint.study.mapper.KnowledgeTypeMapper;
import com.knowledgepoint.study.model.entity.KnowledgeType;
import com.knowledgepoint.study.service.KnowledgeTypeService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 知识点类型 Service 实现
 */
@Service
public class KnowledgeTypeServiceImpl extends ServiceImpl<KnowledgeTypeMapper, KnowledgeType> implements KnowledgeTypeService {

    @Override
    public List<KnowledgeType> listEnabled() {
        LambdaQueryWrapper<KnowledgeType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KnowledgeType::getStatus, 1) // 1 表示启用状态
                   .orderByAsc(KnowledgeType::getSort);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean isNameExist(String name, Long excludeId) {
        LambdaQueryWrapper<KnowledgeType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KnowledgeType::getName, name);
        if (excludeId != null) {
            queryWrapper.ne(KnowledgeType::getId, excludeId);
        }
        return baseMapper.selectCount(queryWrapper) > 0;
    }
    
    @Override
    public <P extends IPage<KnowledgeType>> P page(P page, Wrapper<KnowledgeType> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }
}
